﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
namespace vKapse.DDTool.Core
{

	#region Using

	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Windows.Forms;

	#endregion Using

	/// <summary>
	/// Asociace pro ToolBarItem
	/// </summary>
	internal class ToolBarItemAssociation : TriggerAssociationBase
	{

		#region Constructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		internal ToolBarItemAssociation() 
			: base(typeof(ToolStripItem))
		{
		}

		#endregion Constructor

		#region Methods

		internal override void ApplyChanges(TriggerBase trigger, object obj)
		{
			ToolStripItem item = (ToolStripItem)obj;
			item.Text = trigger.Text;
			item.Image = trigger.Image;
			item.ToolTipText = trigger.ToolTipText;
			item.Visible = trigger.Visible;
			item.Enabled = trigger.Enabled;
			if (trigger.IsCheckBox)
			{
				(item as ToolStripButton).CheckOnClick = true;
				(item as ToolStripButton).Checked = trigger.IsChecked;
			}
		}

		internal override void Register(TriggerBase trigger, object obj)
		{
			this.Add(trigger, obj);
			ToolStripItem item = (ToolStripItem)obj;
			item.Click += new EventHandler(this.Item_Click);
			if (trigger.IsCheckBox)
			{
				(item as ToolStripButton).CheckedChanged += new EventHandler(CheckedChanged);
			}
		}

		private void CheckedChanged(object sender, EventArgs e)
		{
			TriggerBase trigger = GetTrigger(sender);
			trigger.IsChecked = (sender as ToolStripButton).Checked;
		}

		internal override void Unregister(object obj)
		{
			ToolStripItem item = (ToolStripItem)obj;
			item.Click -= new EventHandler(this.Item_Click);
			TriggerBase trigger = this.GetTrigger(obj);
			if(trigger.IsCheckBox)
			{
				(item as ToolStripButton).CheckedChanged -= new EventHandler(CheckedChanged);
			}
			this.Remove(obj);
		}

		#endregion Methods

		#region Event Reactions

		private void Item_Click(object sender, EventArgs e)
		{
			this.GetTrigger(sender).Run();
		}

		#endregion Event Reactions

	}
}
